package com.portal.caresms;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.caresms.model.Category_Master;
import com.caresms.model.Keyword_Master;
import com.caresms.service.Category_MasterLocalServiceUtil;
import com.caresms.service.Keyword_MasterLocalServiceUtil;
import com.caresms.service.Keyword_Sms_RelationLocalServiceUtil;
import com.caresms.service.Sms_MasterLocalServiceUtil;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class Smscontrol extends MVCPortlet {
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		long keyId = ParamUtil.getLong(resourceRequest, "keyId");
		String action = ParamUtil.getString(resourceRequest, "action");
		if (action.equalsIgnoreCase("first")) {
			System.out.println("call serveResource Method");
			String keyname = null;
			JSONArray dataarray = JSONFactoryUtil.getJSONFactory()
					.createJSONArray();

			JSONObject json1 = JSONFactoryUtil.createJSONObject();

			long keywordid = 0l;

			try {
				List<Keyword_Master> l = Keyword_MasterLocalServiceUtil
						.getByCatId(keyId);

				for (int i = 0; i < l.size(); i++) {
					// JSONObject listEntry =
					// JSONFactoryUtil.createJSONObject();
					keyname = l.get(i).getKeywordName();
					keywordid = l.get(i).getKeyId();
					// listEntry.put("data", keyname);
					dataarray.put(keywordid + ":" + keyname);

				}
				json1.put("dataarray", dataarray);

			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resourceResponse.setContentType("application/json");
			resourceResponse.setCharacterEncoding("UTF-8");
			resourceResponse.getWriter().write(json1.toString());
		}

	}

	public void smsAction(ActionRequest aq, ActionResponse ar) {
		ThemeDisplay themeDisplay = (ThemeDisplay) aq
				.getAttribute(WebKeys.THEME_DISPLAY);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		Date d = new Date();
		int count=0;
		long userid = themeDisplay.getUserId();
		long created = themeDisplay.getUserId();
		long companyid = themeDisplay.getCompanyId();
		String s = ParamUtil.getString(aq, "hdnview");
		String chars = ParamUtil.getString(aq, "hdnsize");
		int cs = Integer.valueOf(chars);
		String content = ParamUtil.getString(aq, "meTextarea");
	
		List<String> keylist = new ArrayList<String>(
				Arrays.asList(s.split(",")));
		List<String> l1 = new ArrayList<String>();
		boolean active = ParamUtil.getBoolean(aq, "isActive");
		long smsid = Sms_MasterLocalServiceUtil.addsms(companyid, userid,
				content, created, active, cs);
		int check=0;
		for (String ss : keylist) {
			String keyword = ss.trim();

			if (!keyword.isEmpty()) {
				long keyid = Long.valueOf(keyword);
				try {
					Keyword_Master skm = Keyword_MasterLocalServiceUtil
							.getKeyword_Master(keyid);
					if(skm.getField3()==check)
					{
						skm.setField3(1);
						Keyword_MasterLocalServiceUtil.updateKeyword_Master(skm);
					}
					//System.out.println("catid"+skm.getCat_Id()+"keyid"+keyid);
					l1.add(""+skm.getCat_Id());
					long categoryid=skm.getCat_Id();
					Keyword_Sms_RelationLocalServiceUtil.addsmskeyid(smsid,keyid,categoryid);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (com.liferay.portal.kernel.exception.SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
			 Set<String> set=new HashSet<String>(l1);
			 for (String string : set) {
				 System.out.println("catid"+string.trim());
				 String c=string.trim();
				 long cid = Long.valueOf(c);
				 try {
					Category_Master cm=Category_MasterLocalServiceUtil.getCategory_Master(cid);
					count=cm.getCountsms();
					System.out.println("count"+count);
					count=count+1;
					System.out.println("count"+count);
					cm.setCountsms(count);
					Category_MasterLocalServiceUtil.updateCategory_Master(cm);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (com.liferay.portal.kernel.exception.SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 }
	
	}
}
